package com.qkjia.config;

import com.baomidou.mybatisplus.core.injector.ISqlInjector;
import com.baomidou.mybatisplus.extension.injector.LogicSqlInjector;
import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * @Auther 赵乾坤
 * @Date 2021-06-24 21:29
 */
@Configuration//配置类
@EnableTransactionManagement//开启事务管理
@MapperScan("com.qkjia.mapper")
@ApiModel("插件配置类----------->用法在测试类里面")
public class MybatisPlusConfig {


    @Bean
    @ApiModelProperty("乐观锁插件")
    public OptimisticLockerInterceptor optimisticLockerInterceptor() {
        return new OptimisticLockerInterceptor();
    }


    @Bean
    @ApiModelProperty("分页插件")
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }

    @Bean
    @ApiModelProperty("逻辑删除插件")
    public ISqlInjector sqlInjector() {
        return new LogicSqlInjector();
    }

    /**
     * SQL 执行性能分析插件
     * 开发环境使用，线上不推荐。 maxTime 指的是 sql 最大执行时长
     * 可以针对各环境新建不同的配置文件application-dev.properties、application-test.properties、application-prod.properties
     * 也可以自定义环境名称：如test1、test2
     */
    @ApiModelProperty("执行性能分析插件     ------------>超过100ms不执行")
    @Bean
    @Profile({"dev","test"})// 设置 dev test 环境开启
    public PerformanceInterceptor performanceInterceptor() {
        PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
        performanceInterceptor.setMaxTime(100);//ms，超过此处设置的ms则sql不执行
//        performanceInterceptor.setMaxTime(1);//ms，超过此处设置的ms则sql不执行
//        ### Cause: com.baomidou.mybatisplus.core.exceptions.MybatisPlusException:  The SQL execution time is too large, please optimize !
                performanceInterceptor.setFormat(true);
        return performanceInterceptor;
    }

}
